﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GLImp;
using System.Drawing;
using OpenTK;
using CastleOfTheDisillusioned.Overworld.Tiles;

namespace CastleOfTheDisillusioned.Overworld {
	class Realm {
		Tile[,] Tiles;
		public int Width;
		public int Height;

		public Realm(int Width, int Height) {
			Tiles = new Tile[Width, Height];
			for (int x = 0; x < Width; x++) {
				for (int y = 0; y < Height; y++) {
					//Tiles[x, y] = new Tile(x, y);
					if (Program.Random.Next(3) > 0) {
						Tiles[x, y] = new ForrestTile(x, y);
					} else {
						Tiles[x, y] = new MountainTile(x, y);
					}
				}
			}
			Tiles[6, 5] = new PrisonTile(6,5);
			this.Width = Width;
			this.Height = Height;
		}

		public void Draw() {
			foreach (Tile t in Tiles) {
				if (t.Visible) {
					t.Draw();
				}

				if (!t.Visited) {
					GraphicsManager.DrawRectangle(t.x, t.y, Tile.Width, Tile.Height, Color.FromArgb(200, 0, 0, 0));
				}
			}

			//Draw Grid
			for (int x = 0; x <= Width; x++) {
				GraphicsManager.DrawLine(new Vector2d(x, 0), new Vector2d(x, Height), Color.Black);
			}
			for (int y = 0; y <= Height; y++) {
				GraphicsManager.DrawLine(new Vector2d(0, y), new Vector2d(Width, y), Color.Black);
			}
		}

		//Explores wherever the player currently is
		public void Explore() {
			Explore(Overworld_State.Player.X, Overworld_State.Player.Y);
		}

		//Marks tile at location as visited, and makes surrounding tiles visibile
		public void Explore(int X, int Y) {
			Tiles[X, Y].Visited = true;

			for (int i = -1; i <= 1; i++) {
				MakeVisible(X + i, Y);
				MakeVisible(X, Y + i);
			}
		}

		public void MakeVisible(int X, int Y) {
			if (OutsideBounds(X, Y)) return;
			Tiles[X, Y].Visible = true;
		}

		public Tile GetTile(int X, int Y) {
			return Tiles[X, Y];
		}

		public bool PlaceVisible(int X, int Y) {
			if (OutsideBounds(X, Y)) return false;
			return Tiles[X, Y].Visible;
		}

		private bool OutsideBounds(int X, int Y) {
			return X < 0 || X >= Width || Y < 0 || Y >= Height;
		}
	}
}
